export interface Point {
  lat: number
  lng: number
}

/*
 * 计算两个坐标点之间的距离
 */
export function calculation(point: Point, lastPoint: Point) {
  const radLat1 = (point.lat * Math.PI) / 180.0
  const radLat2 = (lastPoint.lng * Math.PI) / 180.0
  const y = radLat1 - radLat2
  const x = (point.lat * Math.PI) / 180.0 - (lastPoint.lng * Math.PI) / 180.0

  const result =
    2 *
    6378.137 *
    Math.asin(
      Math.sqrt(
        Math.pow(Math.sin(y / 2), 2) +
          Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(x / 2), 2)
      )
    )

  // 将计算结果转化为 m,保留两位小数
  return +(result * 1000).toFixed(2)
}
